package com.rwx.myjetpack.musiclist

import com.rwx.myjetpack.domain.Music
import com.rwx.myjetpack.lifecycle.AbsLifecycle
import com.rwx.myjetpack.lifecycle.ILifecycle
import com.rwx.myjetpack.lifecycle.ILifecycleOwner
import com.rwx.myjetpack.lifecycle.LifeState
import com.rwx.myjetpack.player.DataListenerContainer

/**
 * Created by LH on 2023-05-26.
 * Time: 2023-05-26  10:39
 */
class MusicListPresenter(owner:ILifecycleOwner) /*:ILifecycle*/{

    enum class MusicLoadState{
        LOADDIN,EMPTY,SUCCESS,ERROR
    }

    private val viewLifeImpl by lazy { ViewLifeImpl() }

    init {
        owner.getLifecycleProvider().addLifeListener(viewLifeImpl)
    }

    private val musicModel by lazy { MusicListModel() }

    val musicList=DataListenerContainer<List<Music>>()
    val loadState=DataListenerContainer<MusicLoadState>()

    private val page=1
    private val size=30

    fun getMusic() {
        loadState.value=MusicLoadState.LOADDIN
        musicModel.loadMusicByPage(page, size,object : MusicListModel.onMusicLoadResult{
            override fun onSuccess(result: List<Music>) {
                musicList.value=result
                loadState.value=if(result.isEmpty()){
                    MusicLoadState.EMPTY
                }else{
                    MusicLoadState.SUCCESS
                }

            }

            override fun onFiled(msg: String, code: Int) {

                loadState.value=MusicLoadState.ERROR

                println("error....${msg}....${code}")
            }

        })

    }

    inner class ViewLifeImpl: AbsLifecycle() {

        override fun onCreate() {


        }

        override fun onStart() {
            println("检测~~~ 检测网络变化")

        }

        override fun onResume() {

        }

        override fun onPause() {

        }

        override fun onStope() {
            println("检测~~~ 停止检测")
        }

        override fun onDestory() {

        }

        override fun onViewLifeStateChange(state: LifeState) {

            println("current state $state")
        }
    }


}